/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import es.upm.dit.gsi.eclipse.jadex.util.ImageFactory;
import es.upm.dit.gsi.eclipse.jadex.common.util.*;

/**
 * Wizard for exporting a capability to a custom destination
 * 
 * 
 * @author Mario Moreno
 * 
 */
public class ExportCapabilityToWizard extends Wizard implements IExportWizard {
	private ExportCapabilityToWizardPage page1;
	private ISelection selection;


	public ExportCapabilityToWizard() {
		super();
		setNeedsProgressMonitor(true);
		setDefaultPageImageDescriptor(ImageFactory.getImageDescriptor(ImageFactory.GSI_BANNER_SMALL));
	}

	/**
	 * Adding the page to the wizard.
	 */
	public void addPages() {
		page1 = new ExportCapabilityToWizardPage();
		addPage(page1);
	}
	
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
	
	/**
	 * This method is called when 'Finish' button is pressed in
	 * the wizard. We will create an operation and run it
	 * using wizard as execution context.
	 */
	public boolean performFinish() {		
		
		//This compress the selected capability into a zip and saves it in the selected
		//location
		final String path = ExportCapabilityToWizardPage.getCapabilityPath();
		final String destinationDirectory = ExportCapabilityToWizardPage.getDestinationDirectory();
		
		
		
		
				
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					doFinish(path, destinationDirectory, monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
	}
	
	/**
	 * The worker method. It will add the necessary information to the ADF file in 
	 * order to configure the new imports.
	 */
	private void doFinish(String path,
			String destinationDirectory,
			IProgressMonitor monitor)
		throws CoreException {
		
		monitor.beginTask("Creating and saving zip file", 2);
		
		List<String> list = new ArrayList<String>();
		list.add(path);
		
		try {
			ZipCompressor.compress(list, destinationDirectory);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    System.out.println("Done");
	    monitor.worked(1);
	}

//	@Override
//	public boolean performFinish() {
//		String path = ExportCapabilityToWizardPage.getCapabilityPath();
//		String destinationDirectory = ExportCapabilityToWizardPage.getDestinationDirectory();
//		
//		List<String> list = new ArrayList<String>();
//		list.add(path);
//		
//		try {
//			ZipCompressor.compress(list, destinationDirectory);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//			
//		return false;
//	}

}
